//
//  YMJEffectView.m
//  MJShareView
//
//  Created by 殷殷明静 on 16/9/26.
//  Copyright © 2016年 MInJIng_Lin. All rights reserved.
//

#import "YMJEffectView.h"
#import "CustomButton.h"
#define LSCREENH [UIScreen mainScreen].bounds.size.height
#define LSCREENW [UIScreen mainScreen].bounds.size.width
#define ShareH 150
@implementation YMJEffectView
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
                CustomButton * btn1 = [self btnAnimateWithFrame:CGRectMake(LSCREENW/4-50, LSCREENH+ 80, 60, 60) imageName:@"sns_icon_23" title:@"朋友圈"  animateFrame:CGRectMake(LSCREENW/4-50, 130, 60, 60) delay:0.0];
                btn1.tag = 1;
                [btn1 addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
      
        CustomButton * btn2 = [self btnAnimateWithFrame:CGRectMake(LSCREENW/4-30 +60 + 20, LSCREENH + 80, 60, 60) imageName:@"sns_icon_22" title:@"微信" animateFrame:CGRectMake(LSCREENW/4-30 +60 + 20, 130, 60, 60) delay:0.1];
        btn2.tag=2;
        [btn2 addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside];

        CustomButton * btn3 = [self btnAnimateWithFrame:CGRectMake(LSCREENW/4-30 +(60 + 30) * 2, LSCREENH + 80, 60, 60) imageName:@"sns_icon_24" title:@"QQ" animateFrame:CGRectMake(LSCREENW/4-30 +(60 + 30) * 2, 130, 60, 60) delay:0.2];
        btn3.tag=3;
        [btn3 addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        CustomButton * btn4 = [self btnAnimateWithFrame:CGRectMake(LSCREENW/4-50, LSCREENH + 80 , 60, 60) imageName:@"sns_icon_6" title:@"QQ空间" animateFrame:CGRectMake(LSCREENW/4-50, 220, 60, 60) delay:0.3];
        btn4.tag=4;
        [btn4 addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        CustomButton * btn5 = [self btnAnimateWithFrame:CGRectMake(LSCREENW/4-30 +60 + 20, LSCREENH + 80 , 60, 60) imageName:@"sns_icon_1" title:@"新浪微博" animateFrame:CGRectMake(LSCREENW/4-30 +60 + 20, 220, 60, 60) delay:0.4];
        btn5.tag=5;
        [btn5 addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        CustomButton * btn6 = [self btnAnimateWithFrame:CGRectMake(LSCREENW/4-30 +(60 + 30) * 2, LSCREENH + 80 , 60, 60) imageName:@"jubao" title:@"举报" animateFrame:CGRectMake(LSCREENW/4-30 +(60 + 30) * 2, 220, 60, 60) delay:0.5];
        btn6.tag=6;
        [btn6 addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        CustomButton * btn7 = [self btnAnimateWithFrame:CGRectMake(LSCREENW/4-50, LSCREENH + 80 , 60, 60) imageName:@"lianjieCopy" title:@"复制链接" animateFrame:CGRectMake(LSCREENW/4-50, 310, 60, 60) delay:0.6];
        btn7.tag=7;
        [btn7 addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        CustomButton * btn8 = [self btnAnimateWithFrame:CGRectMake(LSCREENW/4-30 +60 + 20, LSCREENH + 80 , 60, 60) imageName:@"restore" title:@"收藏" animateFrame:CGRectMake(LSCREENW/4-30 +60 + 20, 310, 60, 60) delay:0.7];
        btn8.tag=8;
        [btn8 addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        
        
//        UIButton *plus = [UIButton buttonWithType:UIButtonTypeCustom];
//        plus.frame = CGRectMake((LSCREENW-25)/2 ,LSCREENH-35, 25, 25);
//        [plus setImage:[UIImage imageNamed:@"close_share_icon"] forState:UIControlStateNormal];
//        [plus addTarget:self action:@selector(cancelAnimation) forControlEvents:UIControlEventTouchUpInside];
//        plus.tag =3;
//        [self addSubview:plus];
//        [UIView animateWithDuration:0.2 animations:^{
//            
//            plus.transform = CGAffineTransformMakeRotation(M_PI_2);
//        }];

        
        
    }
    return self;
}
-(void)show
{
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    [keyWindow addSubview:self];
}
-(CustomButton *)btnAnimateWithFrame:(CGRect)frame imageName:(NSString *)imageName title:(NSString *)title animateFrame:(CGRect)aniFrame delay:(CGFloat)delay
{
    CustomButton * btn =[CustomButton buttonWithType:UIButtonTypeCustom];
    btn.frame =frame;
    [btn setImage:[UIImage imageNamed:imageName] forState:0];
    [btn setTitle:title forState:0];
    [btn setTitleColor:[UIColor blackColor] forState:0];
    btn.titleLabel.font = [UIFont systemFontOfSize:14];
    [self  addSubview:btn];
    [btn verticalImageAndTitle:10];
    [UIView animateWithDuration:1 delay:delay usingSpringWithDamping:0.7 initialSpringVelocity:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
        
        btn.frame  = aniFrame;
        
    } completion:^(BOOL finished) {
        
    }];
    return btn;
    
    //usingSpringWithDamping :弹簧动画的阻尼值，也就是相当于摩擦力的大小，该属性的值从0.0到1.0之间，越靠近0，阻尼越小，弹动的幅度越大，反之阻尼越大，弹动的幅度越小，如果大道一定程度，会出现弹不动的情况。
    //initialSpringVelocity :弹簧动画的速率，或者说是动力。值越小弹簧的动力越小，弹簧拉伸的幅度越小，反之动力越大，弹簧拉伸的幅度越大。这里需要注意的是，如果设置为0，表示忽略该属性，由动画持续时间和阻尼计算动画的效果。
    
}
-(void)BtnClick:(UIButton*)btn
{
    for (NSInteger i = 0; i<self.subviews.count; i++)
    {
        UIView *view = self.subviews[i];
        if ([view isKindOfClass:[UIButton class]])
        {
            [UIView animateWithDuration:0.3 delay:0.1*i options:UIViewAnimationOptionTransitionCurlDown animations:^{
                view.frame = CGRectMake(view.frame.origin.x, LSCREENH, 60, 60);
            } completion:^(BOOL finished) {
            }];
        }
    }
    
    [self performSelector:@selector(removeView:) withObject:btn afterDelay:0];
    /*面板延迟0.5秒才消失_(原)改为了 0秒,瞬间消失*/
}
-(void)removeView:(UIButton*)btn
{
    [self removeFromSuperview];
    [self.delegate didSelectBtnWithBtnTag:btn.tag];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    
    CGPoint currentPosition = [touch locationInView:self];
    
    CGFloat deltaY = currentPosition.y;
    if (deltaY<(LSCREENH-ShareH))
    {
        [self cancelAnimation];
    }
}
-(void)cancelAnimation
{
    UIButton * cancelBtn =(UIButton*)[self viewWithTag:3];
    [UIView animateWithDuration:0.2 animations:^{
        
        cancelBtn.transform = CGAffineTransformMakeRotation(-M_PI_2);
    }];
    
    for (NSInteger i = 0; i<self.subviews.count; i++)
    {
        
        [self removeFromSuperview];
        /*原动画依次消失,现改成瞬间消失并且移除*/
//        UIView *view = self.subviews[i];
//        if ([view isKindOfClass:[UIButton class]])
//        {
//            [UIView animateWithDuration:0.3 delay:0.1*i options:UIViewAnimationOptionTransitionCurlDown animations:^{
//                view.frame = CGRectMake(view.frame.origin.x, LSCREENH, 60, 60);
//            } completion:^(BOOL finished) {
//                
//                [self removeFromSuperview];
//            }];
//        }
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
